<?php

namespace app\common\model\shop;

use think\Model;
use traits\model\SoftDelete;

class Goods extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'shop_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'images_url',
        'display_text',
        'image_text'
    ];
    

    
    public function getDisplayList()
    {
        return ['0' => __('Display 0'), '1' => __('Display 1')];
    }


    public function getDisplayTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['display']) ? $data['display'] : '');
        $list = $this->getDisplayList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getImageTextAttr($value, $data){
        $value = $value ? $value : (isset($data['image']) ? $data['image'] : '');

        return cdnurl($value, true);
    }

    public function getImagesUrlAttr($value, $data){
        $images = isset($data['images']) ? explode(",", $data['images']) : [];
        foreach ($images as $key => &$value) {
            $value = cdnurl($value, true);
        }
        return $images;
    }


    public function category()
    {
        return $this->belongsTo('app\common\model\shop\Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

}
